package com.anstudio.app.lhasa.service.controllers;

import com.anstudio.app.lhasa.common.enums.ResponseEnum;
import com.anstudio.app.lhasa.common.modules.Location;
import com.anstudio.app.lhasa.common.modules.Response;
import com.anstudio.app.lhasa.service.workers.SightsWorker;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.anstudio.app.lhasa.common.enums.ResponseEnum.RESP_EXCEPTION;
import static com.anstudio.app.lhasa.common.modules.Response.encode;

@RestController
public class SightController {
    @RequestMapping(value = "/sight", method = RequestMethod.GET)
    public Response getAllSights(HttpServletResponse response) {
        Response resp = new Response();
        try {
            resp.setResult(ResponseEnum.RESP_OK);
            resp.setBody(encode(SightsWorker.instance.getAllSightInformation()));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            resp.setResult(RESP_EXCEPTION);
        }
        return resp;
    }

    @RequestMapping(value = "/sight/{id}", method = RequestMethod.GET)
    public Response getSightById(@PathVariable int id, HttpServletResponse response) {
        Response resp = new Response();
        try {
            resp.setResult(ResponseEnum.RESP_OK);
            resp.setBody(encode(SightsWorker.instance.getSightInformationById(id)));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            resp.setResult(RESP_EXCEPTION);
        }

        return resp;
    }

    @RequestMapping(value = "/sight/name/{name}", method = RequestMethod.GET)
    public Response getSightByIName(@PathVariable String name, HttpServletResponse response) {
        Response resp = new Response();
        try {
            resp.setResult(ResponseEnum.RESP_OK);
            resp.setBody(encode(SightsWorker.instance.getSightIngotmationByName(name)));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            resp.setResult(RESP_EXCEPTION);
        }

        return resp;
    }

    @RequestMapping(value = "/sight/{latitude}/{longitude}", method = RequestMethod.GET)
    public Response getSightByGPS(
            @PathVariable float latitude, @PathVariable float longitude, HttpServletResponse response) {
        Response resp = new Response();

        try {
            resp.setResult(ResponseEnum.RESP_OK);
            resp.setBody(encode(SightsWorker.instance.getSightInformationByGPS(latitude, longitude)));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            resp.setResult(RESP_EXCEPTION);
        }

        return resp;
    }

    @RequestMapping(value = "/sight/location", method = RequestMethod.POST)
    public Response getSightByLoc(@RequestBody String body, HttpServletResponse response) {
        Response resp = new Response();
        JSONObject obj;
        Response ret = null;
        try {
            obj = new JSONObject(body);
            if (obj == null) {
                return ret;
            }

            Location location = new ObjectMapper().readValue(body, Location.class);

            resp.setResult(ResponseEnum.RESP_OK);
            resp.setBody(encode(SightsWorker.instance.getSightInformationByGPS(
                    location.getLatitude(), location.getLongitude())));
        } catch (JSONException e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return new Response().setResult(RESP_EXCEPTION).setBody(e.toString());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            resp.setResult(RESP_EXCEPTION);
        } catch (IOException e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return new Response().setResult(RESP_EXCEPTION).setBody(e.toString());
        }

        return resp;
    }
}
